package com.peng.gulimall.gateway.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class GulimallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //允许哪些头可以跨域，*为所有
        corsConfiguration.addAllowedHeader("*");

        //允许哪些请求方式(get,post等)可以跨域，*为所有
        corsConfiguration.addAllowedMethod("*");

        //允许哪些请求来源可以跨域，*为所有
        corsConfiguration.addAllowedOrigin("*");

        //是否可以携带cookie跨域，true为是
        corsConfiguration.setAllowCredentials(true);
        //”/**“表示所有的请求都要进行跨域配置，且按照corsConfiguration的配置方式执行
        source.registerCorsConfiguration("/**",corsConfiguration);

        return new CorsWebFilter(source);
    }
}
